﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.Domain.Entities;
using Sgr.EntityFrameworkCore.EntityConfigurations;
using Sgr.UPMS.Domain.Organizations;

namespace Sgr.UPMS.Infrastructure.EntityConfigurations
{
    /// <summary>
    /// 组织邀请实体配置
    /// </summary>
    internal class OrganizationInvitationEntityTypeConfiguration
        : EntityTypeConfigurationBase<OrganizationInvitation, long>
    {
        public OrganizationInvitationEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<OrganizationInvitation> builder)
        {
            builder.ToTable("upms_org_invitations");

            base.Configure(builder);

            // 组织ID - 必填
            builder.Property(b => b.OrganizationId)
                .IsRequired();

            // 邀请码 - 必填，唯一索引，最大长度20
            builder.Property(b => b.InvitationCode)
                .IsRequired()
                .HasMaxLength(20);

            // 过期时间 - 必填
            builder.Property(b => b.ExpireTime)
                .IsRequired();

            // 可使用次数 - 必填，默认-1（不限制）
            builder.Property(b => b.UsageLimit)
                .IsRequired()
                .HasDefaultValue(-1);

            // 已使用次数 - 必填，默认0
            builder.Property(b => b.UsedCount)
                .IsRequired()
                .HasDefaultValue(0);

            // 描述 - 可选，最大长度500
            builder.Property(b => b.Description)
                .HasMaxLength(500);

            // 状态 - 必填，默认值Normal
            builder.Property(b => b.State)
                .IsRequired();

            // 创建索引
            builder.HasIndex(b => new { b.OrganizationId });
            builder.HasIndex(b => b.InvitationCode).IsUnique();
        }
    }
}